﻿-- =============================================
-- Author:		<赵洪涛>
-- Create date: <2012-08-20>
-- Description:	<InSide转出客户给接口人>
-- =============================================

CREATE PROCEDURE [dbo].[proc_InOutSide_OutCustomers]
(
	@CusId uniqueidentifier,
	@CompanyId int,
	@InSide varchar(20)
)
AS
BEGIN
	Declare @ProtectDate Datetime,@nCallCount int
	Set @nCallCount = 0

	--取该客户的起始保护日期
	Select @ProtectDate=ProtectDate From Customer Where CustomerId=@CusId And ProtectId=2 And ProtectName = @InSide
	If @ProtectDate Is Null
	Begin
		Select -2 As ReturnValue
		Return		--未写保护
	End

	--该客户所有的联系电话
--	;WITH List As(
--		Select Mobile As Tel
--		From ContactPerson
--		Where CustomerId=@CusId
--		And Mobile<>'' And (Mobile Is Not Null)
--		Union
--		Select OfficeTel As Tel
--		From ContactPerson
--		Where CustomerId=@CusId
--		And OfficeTel<>'' And (OfficeTel Is Not Null)
--		Union
--		Select HomeTel As Tel
--		From ContactPerson
--		Where CustomerId=@CusId
--		And HomeTel<>'' And (HomeTel Is Not Null)
--	)

	--取该客户保护日期开始的所有呼叫记录
--	Select @nCallCount=Count(0) From CallCenter_Record a,List b Where a.CompanyId=@CompanyId And EndDate>@ProtectDate And a.Tel=b.Tel
--	If @nCallCount=0
--	Begin
--		Select -1 As ReturnValue
--		Return
--	End

	Insert Into InOutSide(CusId,CompanyId,InSide,InSide_ProtectDate) Values(@CusId,@CompanyId,@InSide,@ProtectDate)
	Update Customer Set ProtectName = '' Where CustomerId=@CusId

	Select 1 As ReturnValue
	Return
END
